/** */
package co.com.umobile.service;

import android.accounts.Account;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import co.com.umobile.application.GlobalState;
import co.com.umobile.common.util.ServicesCodes;
import co.com.umobile.domain.User;
import co.com.umobile.persistence.DatabaseManager;
import co.com.umobile.persistence.ProviderUtilities;


/**
 * @author sergiogranada
 * 
 */
public class SubjectProvider extends ContentProvider {

  /** The uri matcher. */
  private static final UriMatcher URI_MATCHER = buildUriMatcher();

  /**
   * Builds up a UriMatcher for search suggestion and shortcut refresh queries.
   * @return UriMatcher.
   */
  private static UriMatcher buildUriMatcher() {
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // to get definitions...
    matcher.addURI(ProviderUtilities.SUBJECT_AUTORITY, "subjectList", ServicesCodes.SUBJECT_UPDATE);

    return matcher;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[])
   */
  @Override
  public int delete(Uri uri, String arg1, String[] arg2) {
    switch (URI_MATCHER.match(uri)) {
      case ServicesCodes.SUBJECT_UPDATE:
        return 0;

      default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#getType(android.net.Uri)
   */
  @Override
  public String getType(Uri uri) {
    return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)
   */
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    switch (URI_MATCHER.match(uri)) {
      case ServicesCodes.SUBJECT_UPDATE:
        return null;
      default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#onCreate()
   */
  @Override
  public boolean onCreate() {
    DatabaseManager.getInstance(getContext());
    return true;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String,
   * java.lang.String[], java.lang.String)
   */
  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    User userSession;
    Account account;
    Bundle extras;

    switch (URI_MATCHER.match(uri)) {
      case ServicesCodes.SUBJECT_UPDATE:

        userSession = ((GlobalState) getContext().getApplicationContext()).getLastUserSession();

        if (userSession == null || userSession.getUserCode().length() == 0) {
          return null;
        }

        account = new Account(userSession.getUserCode(), ProviderUtilities.ACCOUNT_TYPE);
        extras = new Bundle();
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        
        ContentResolver.requestSync(account, ProviderUtilities.SUBJECT_AUTORITY, extras);

        return null;

      default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues,
   * java.lang.String, java.lang.String[])
   */
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    switch (URI_MATCHER.match(uri)) {
      case ServicesCodes.SUBJECT_UPDATE:
        return 0;

      default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
  }

}
